﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hsmp.SocietyAdmin.IBusinessLogic;
using Hsmp.SocietyAdmin.BusinessLogic;
using Hsmp.SocietyAdmin.Entity;
using HSMPWeb.ViewEntity;

namespace HSMPWeb.UserControls
{
    public partial class SocietyDetailsControlControl : System.Web.UI.UserControl
    {
        #region  Variable & constant
        string lCreatedByID = "SysAdmin";
        //Society _Society;
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckPageAuthorization())
            {
                string lQueryStringParameterSocietyKey = Request.QueryString["SocietyId"];
                if (!string.IsNullOrEmpty(lQueryStringParameterSocietyKey))
                {
                    if (!IsPostBack)
                    {
                        Society lSociety = new Society();
                        lSociety = GetselectedSociety(lQueryStringParameterSocietyKey);
                        Session["SelectedSociety"] = lSociety;
                        FillScreen(lSociety);
                        //this.lblPageCaption.Text = "Edit Society";
                        btnRegisterSociety.Visible = false;
                        btnUpdate.Visible = true;
                    }
                }
                else
                {
                    //this.lblPageCaption.Text = "New Society Registration";
                    btnRegisterSociety.Visible = true;
                }
            }
            else
            {
            }

        }

        protected void btnRegisterSociety_Click(object sender, EventArgs e)
        {

            try
            {
                Society lSociety = new Society();
                lSociety.SocietyCode = this.txtSocietyCode.Text;
                lSociety.SocietyName = this.txtSocietyName.Text;
                lSociety.SocietyAddressLine1 = this.txtAddresLine1.Text;
                lSociety.SocietyAddressLine2 = this.txtAddressLine2.Text;
                lSociety.SocietyAddressCity = this.txtCity.Text;
                lSociety.SocietyAddressState = this.txtState.Text;
                lSociety.SocietyAddressCountry = this.txtCountry.Text;
                lSociety.SocietyAddressZip = this.txtZip.Text;
                lSociety.Sys_CreatedByID = lCreatedByID;

                SocietyRegistrationBus lSocietyRegistrationBus = new SocietyRegistrationBus();
                lSocietyRegistrationBus.RegisterNewSociety(lSociety);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                //Need to display the exception message to the user or log it in application log.
            }

            btnRegisterSociety.Visible = false;
            btnUpdate.Visible = true;


            //lblText.Text = txtSocietyName.Text + " " + "Successfully Registered !!!";
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {

                Society lSociety = new Society();
                lSociety = (Society)Session["SelectedSociety"];
                lSociety.SocietyCode = this.txtSocietyCode.Text;
                lSociety.SocietyName = this.txtSocietyName.Text;
                lSociety.SocietyAddressLine1 = this.txtAddresLine1.Text;
                lSociety.SocietyAddressLine2 = this.txtAddressLine2.Text;
                lSociety.SocietyAddressCity = this.txtCity.Text;
                lSociety.SocietyAddressState = this.txtState.Text;
                lSociety.SocietyAddressCountry = this.txtCountry.Text;
                lSociety.SocietyAddressZip = this.txtZip.Text;
                lSociety.Sys_CreatedByID = lCreatedByID;

                SocietyRegistrationBus lSocietyRegistrationBus = new SocietyRegistrationBus();
                lSocietyRegistrationBus.UpdateRegisteredSociety(lSociety);

                string message = "Are You Want To Update.";
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("<script type = 'text/javascript'>");
                sb.Append("window.onload=function(){");
                sb.Append("alert('");
                sb.Append(message);
                sb.Append("')};");
                sb.Append("</script>");
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());



            }

            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                //Need to display the exception message to the user or log it in application log.
            }

        }
        #endregion Events

        #region Private Methods
        private bool CheckPageAuthorization()
        {
            bool IsUserAuthorized = false;
            if (Session["UserContext"] != null)
            {
                UserContext UserSession = new UserContext();
                UserSession = (UserContext)Session["UserContext"];
                lCreatedByID = UserSession.CurrentUser.UserLoginID;
                //if ((UserSession.UserRole != UserRoles.SocietySuperAdmin) || (UserSession.UserRole != UserRoles.SystemAdmin) || (UserSession.UserRole != UserRoles.SystemOperator) || (UserSession.UserRole != UserRoles.SocietySuperAdmin) || (UserSession.UserRole != UserRoles.SocietyAdmin) || (UserSession.UserRole != UserRoles.SocietyOperator))
                //{
                //    IsUserAuthorized = true;
                //    //Response.Redirect(Request.Url.AbsolutePath);
                //}
                //else
                //{
                //Response.Redirect("~/Pages/AccessDenied.aspx");
                //}
            }
            else
            {
                //Response.Redirect("~/Pages/AccessDenied.aspx");
            }
            return IsUserAuthorized;
        }
        private Society GetselectedSociety(string SocietyKey)
        {
            Society lSociety;
            SocietyRegistrationBus lSocietyRegistrationBus = new SocietyRegistrationBus();
            lSociety = lSocietyRegistrationBus.GetSelectedScoietyByID(SocietyKey);

            return lSociety;
        }

        private void FillScreen(Society pSpciety)
        {
            this.lblSocietyId.Text = pSpciety.SocietyKey.ToString();
            this.txtSocietyCode.Text = pSpciety.SocietyCode;
            this.txtSocietyName.Text = pSpciety.SocietyName;
            this.txtAddresLine1.Text = pSpciety.SocietyAddressLine1;
            this.txtAddressLine2.Text = pSpciety.SocietyAddressLine2;
            this.txtCity.Text = pSpciety.SocietyAddressCity;
            this.txtState.Text = pSpciety.SocietyAddressState;
            this.txtCountry.Text = pSpciety.SocietyAddressCountry;
            this.txtZip.Text = pSpciety.SocietyAddressZip;
        }
        #endregion Private Methods
        
    }
}